﻿using Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using MyProject;
using Service;

namespace Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Book2Controller:BaseController
    {
        public IFreeSql _fsql;

        public Book2Controller(IFreeSql fsql)
        {
            _fsql = fsql;
        }

        [HttpPost]
        public ApiModel Add([FromBody] Book data)
        {
            //var b = new Book() { 
            //    Bookname = data.Bookname, 
            //    CreatName = data.CreatName, 
            //    Publishtime = ConvertDatetime(data.Publishtimestr)
            //};
            var b1 = _fsql.GetRepository<Book>().Insert(data);
            if (b1 == null)
            {
                return apiModel(Status.Error, "插入错误");
            }
            else
            {
                return apiModel(Status.Success, "", b1);
            }
        }

        [HttpPost]
        public ApiModel Update([FromBody] Book data)
        {
            var repo = _fsql.GetRepository<Book>();
            var item = repo.Where(a => a.Id == data.Id).First();  //此时快照 item
            item.CreatName = data.CreatName;
            repo.Update(item); //对比快照时的变化

            if (item == null)
            {
                return apiModel(Status.Error, "更新错误");
            }
            else
            {
                return apiModel(Status.Success, "", item);
            }
        }
    }
}
